﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.DataProvider;
using CuaHangMayTinh.Utilities;
using System.Data;

namespace CuaHangMayTinh.DataAccessLayer
{
    public class GroupUserDA
    {
        public GroupUserDA()
        {

        }

        /// <summary>
        /// Hàm thêm mới 1 SubGroupUserEntity 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Insert(GroupUserEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();
                pb.AddParameter("TenGU", entity.TenGU);

                return (int)DBFactory.Database.ExecuteNonQuery("GroupUser_Insert", pb.Parameters);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Hàm cập nhật 1 SubGroupUserEntity
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpDate(GroupUserEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();
                pb.AddParameter("IDGroupUser", entity.IDGroupUser);
                pb.AddParameter("TenGU", entity.TenGU);

                int numRowEffected = DBFactory.Database.ExecuteNonQuery("GroupUser_Update", pb.Parameters);
                return (numRowEffected > 0);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Hàm tìm tất cả thông tin Quyền
        /// </summary>
        /// <returns></returns>
        public DataTable Find_All()
        {
            try
            {
                return DBFactory.Database.FillDataTable("SELECT * FROM GroupUser");
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Hàm chuyển từ 1 Datatbale chứ thông tin cua2 GroupUser thành 1 List GroupUserEntity
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<GroupUserEntity> ConvertToList(DataTable dt)
        {
            List<GroupUserEntity> list = new List<GroupUserEntity>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int idg = (int)dt.Rows[i][0];
                string tengu = dt.Rows[i][1].ToString();

                GroupUserEntity a = new GroupUserEntity(idg, tengu);
                list.Add(a);
            }
            return list;
        }
    }
}

